package com.cuiweiyou.middleman;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.util.Log;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import dalvik.system.DexClassLoader;

public class PluginManager {
    
    private static PluginManager instance = new PluginManager();
    
    private Context context;
    private DexClassLoader dexClassLoader;
    private PackageInfo packageInfo;
    private Resources resources;
    
    private PluginManager() {
    }
    
    public static PluginManager getInstance() {
        return instance;
    }
    
    /**
     * 传入当前应用(宿主)的ctx
     *
     * @param context 宿主的ctx
     */
    public void setContext(Context context) {
        this.context = context;
        Log.e("ard", "中间人：向我的插件mng放了 context");
    }
    
    /**
     * 加载插件apk文件
     *
     * @param path 文件路径
     */
    public void loadPluginApkFile(String path) {
        File outDexFile = context.getDir("dex", Context.MODE_PRIVATE);                                           // 当前应用(宿主)的内部私有存储路径
        dexClassLoader = new DexClassLoader(path, outDexFile.getAbsolutePath(), null, context.getClassLoader()); // 插件apk的类加载器
        PackageManager packageManager = context.getPackageManager();                                             // 包管理器-系统级单例
        packageInfo = packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);                 // 插件apk内的全部aty
        try {
            Class<AssetManager> assetManagerClass = AssetManager.class;
            AssetManager assetManager = assetManagerClass.newInstance(); // 创建一个AssetManager
            Method addAssetPath = assetManagerClass.getMethod("addAssetPath", String.class);
            addAssetPath.invoke(assetManager, path);                     // 给AssetManager设置apk路径
            Resources resources = context.getResources();
            this.resources = new Resources(assetManager, resources.getDisplayMetrics(), resources.getConfiguration()); // 插件apk的Resource
        } catch (InstantiationException e) {
            Log.e("ard", "中间人：assetsmng实例化错误：" + e.getMessage());
        } catch (IllegalAccessException e) {
            Log.e("ard", "中间人：assetsmng实例反射方法调用非法：" + e.getMessage());
        } catch (NoSuchMethodException e) {
            Log.e("ard", "中间人：assetsmng实例反射方法错误：" + e.getMessage());
        } catch (InvocationTargetException e) {
            Log.e("ard", "中间人：assetsmng实例反射方法调用错误" + e.getMessage());
        }
        Log.e("ard", "中间人：我的插件mng加载了插件apk");
    }
    
    public DexClassLoader getDexClassLoader() {
        Log.e("ard", "中间人：从我的插件mng拿了 DexClassLoader");
        return dexClassLoader;
    }
    
    public PackageInfo getPackageInfo() {
        Log.e("ard", "中间人：从我的插件mng拿了 PackageInfo");
        return packageInfo;
    }
    
    public Resources getResources() {
        Log.e("ard", "中间人：从我的插件mng拿了 Resources");
        return resources;
    }
}
